<?
		session_start();
		require("inc/functions.php");
	 	require_once('inc/facebook.php');
	 	$facebook = new Facebook(array(
	 	'appId' => '266106703516634',
	 	'secret' => '57f64882d938b38d36f88185fc2fd78d'
		));
		
		$fbUser = $facebook->getUser();
		
		if(isset($_POST['login']))
			LoginUser($_POST);
        $searchUrl = "http://search.twitter.com/search.json";
		if(isset($_POST['submit'])){
			$name =strip_tags($_POST['search']);
        	$results = curl_get($searchUrl, "GET", array('q' => $name));
		}
		else
			$results = curl_get($searchUrl, "GET", array('q' => 'New Paltz'));
        //echo $results;
        $ob = json_decode($results);
        //print_r($ob);
        $tweets = $ob->results;
?>

<?php
	include("inc/views/header_view.php");
	include("inc/views/navbar_view.php");
?>

<body>


	<div class="container">
    <? if($fbUser): ?>
		<img src="http://graph.facebook.com/<?=$fbUser?>/picture" />
	 	<a href="<?=$facebook->getLogoutUrl()?>">Log Out</a>
	 	<? else: ?>
	 	<a href="<?=$facebook->getLoginUrl(array('scope' => 'read_stream, friends_likes'))?>">
	 	Log in with Facebook
		</a>
	<? endif; ?>
    	<form class="pull-right" method="post" action="#">
        	<input type="text" name="search" placeholder="Search Twitter" />
        	<input type="submit" name="submit" value="Search" class="btn btn-small btn-success" />
        </form>
		<h3>Searched for <?=$ob->query?></h3>
                                <ul class="unstyled">
                        <? foreach ($tweets as $tweet): ?>
                                          <li id="list_item_<?=$tweet->id?>">
                                          	<div class="well">
                                                <img src="<?=$tweet->profile_image_url?>" />
                                                <a href="https://www.twitter.com/<?=$tweet->from_user?>">@<?=$tweet->from_user?></a>
                                            <?=$tweet->text?>
                                            </div>
                                          </li>
                                        <? endforeach; ?>
                                </ul>  
    </div>
<?php
	include("inc/views/footer_view.php");
?>
<?
        function curl_get($path, $method="GET", $data=null)
        {
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_HEADER, FALSE);
                curl_setopt($curl, CURLOPT_TIMEOUT, 20);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
                curl_setopt($curl, CURLOPT_USERAGENT, 'SUNY New Paltz web course');
                if ($method === 'POST') {
                        curl_setopt($curl, CURLOPT_POST, TRUE);
                        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                }elseif(!empty($data)){
                        if(strpos($path, '?') === false)
                                $path .= '?';
                        else
                                $path .= '&';
                        $path .= http_build_query($data);
                }
                //echo $path;
                curl_setopt($curl, CURLOPT_URL, $path);

                return curl_exec($curl);
        }
?>

